package com.lzx.iteam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.*;

/**
 * Created by Megan on 16/4/7.
 */
public class AttendanceSelectWeekActivity extends Activity implements View.OnClickListener {

    private ListView mLvWeek;
    private TextView mTvOk;
    private LinearLayout mLlBack;
    private WeekAdapter adapter;
    private HashMap<Integer, Boolean> isSelected = new HashMap<Integer, Boolean>();
    private String[] week = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.attendance_select_week_layout);
        initViews();
        getIntentData();
    }

    private void initViews() {
        mLlBack = (LinearLayout) findViewById(R.id.ll_attendance_select_week_back);
        mTvOk = (TextView) findViewById(R.id.tv_attendance_select_week_ok);
        mLvWeek = (ListView) findViewById(R.id.lv_attendance_select_week_list);

        mLlBack.setOnClickListener(this);
        mTvOk.setOnClickListener(this);
        adapter = new WeekAdapter();
        mLvWeek.setAdapter(adapter);
        mLvWeek.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (isSelected.get(position)) {
                    isSelected.put(position, false);
                } else {
                    isSelected.put(position, true);
                }
                adapter.notifyDataSetChanged();
            }
        });

    }


    private void getIntentData() {
        Intent intent = getIntent();
        String weeks[] = intent.getStringExtra("workdate").split(",");
        isSelected.put(0, false);
        isSelected.put(1, false);
        isSelected.put(2, false);
        isSelected.put(3, false);
        isSelected.put(4, false);
        isSelected.put(5, false);
        isSelected.put(6, false);
        for (String week : weeks) {
            if ("1".equals(week)) {
                isSelected.put(0, true);
            } else if ("2".equals(week)) {
                isSelected.put(1, true);
            } else if ("3".equals(week)) {
                isSelected.put(2, true);
            } else if ("4".equals(week)) {
                isSelected.put(3, true);
            } else if ("5".equals(week)) {
                isSelected.put(4, true);
            } else if ("6".equals(week)) {
                isSelected.put(5, true);
            } else if ("7".equals(week)) {
                isSelected.put(6, true);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_attendance_select_week_back:
                finish();
                break;
            case R.id.tv_attendance_select_week_ok:
                /*StringBuilder sb = new StringBuilder();
                for (Map.Entry<Integer, Boolean> entry : isSelected.entrySet()) {
                    if (entry.getValue()) {
                        sb.append(week[entry.getKey()]).append(",");
                    }
                }
                String weeks = sb.substring(0, sb.length() - 1);
                Toast.makeText(this, "所选择的星期有：" + weeks, Toast.LENGTH_LONG).show();*/

                StringBuilder sb = new StringBuilder();

                Object[]   key   =     isSelected.keySet().toArray();
                Arrays.sort(key);
                for   (int   i   =   0;   i   <   key.length;   i++)   {
                    if(isSelected.get(key[i])){
                        sb.append(Integer.parseInt(key[i]+"")+1).append(",");
                    }
                }

                if (sb.length() > 0) {
                    String weeks = sb.substring(0, sb.length() - 1);
                    Intent intent = new Intent();
                    intent.putExtra("week", weeks);
                    setResult(RESULT_OK, intent);
                    Log.d("AttendanceSelectWeekActivity", "所选择的星期有：" + weeks);
                    finish();
                } else {
                    Toast.makeText(this, "对不起，您没有选择工作日期", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }


    private class WeekAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return week.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return week[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(AttendanceSelectWeekActivity.this).inflate(R.layout.item_week_select,
                        parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.text_week.setText(week[position]);
            if (isSelected.get(position)) {
                holder.image_week.setVisibility(View.VISIBLE);
            } else {
                holder.image_week.setVisibility(View.GONE);

            }
            return convertView;
        }
    }

    private class ViewHolder {
        public TextView text_week;
        public ImageView image_week;

        public ViewHolder(View view) {
            text_week = (TextView) view.findViewById(R.id.tv_week_select_text);
            image_week = (ImageView) view.findViewById(R.id.iv_week_select_img);
        }
    }
}
